//var gid = $("#gid").val();
$(document)
		.ready(
				function() {
					lang = $("#display-lang").val();
					var ds_type = $("#ds_type").val();
					var ds_id = $("#ds_id").val();
					var relation_id = $("#relation_id").val();
					var showText;
					if(lang == 'en'){
						showText = "expand";
					}else if(lang == 'zh'){
						showText = "扩展";
					}
					var expandaa= $("<input type='button' class='button-bg' value='"+showText+"' id='expand'></input>");
					
					expandaa.hide();
					$("#pattern_right").html(expandaa);
					var dtitle;
					if(lang == 'en'){
						dtitle = "On Loading";
					}else if(lang == 'zh'){
						dtitle = "正在加载";
					}
					$("#onloading").dialog({
						bgiframe : true,
						autoOpen : false,
						draggable : false,
						closeOnEscape : false,
						closeText : false,
						resizable : false,
						width : 400,
						title : dtitle,
						modal : true
					});
					$("#onloading").parent().appendTo("#dialog_target");
					$('a.ui-dialog-titlebar-close').hide();

					if (relation_id != "0") {
						initialize(relation_id, ds_type, ds_id);
					} else {
						initializeNew();
					}

					$(".del").live("click", function() {
						var data = {};
						data["s"] = $(this).parent().attr("s");
						data["p"] = $(this).parent().attr("p");
						data["o"] = $(this).parent().attr("o");
						data["rel_id"] = relation_id;
						data = JSON.stringify(data);
						data = ("data=" + encodeURIComponent(data));
						$.ajax({
							url : "/sview/tripleDel",
							async : true,
							data : data,
							dataType : "json",
							type : "post",
							success : function(result) {
								getPattern(relation_id);
								getResult(relation_id, ds_type, ds_id);
							}
						});
					});

					$("#pattern_right th").live("mouseenter", function() {
						var i = 0;
						$("#pattern_right th input").each(function() {
							if ($(this).attr("checked") == "checked") {
								i++;
							}
						});
						if (i < 2) {
							$(this).find("input").show();
						}
					});

					$("#pattern_right th")
							.live(
									"mouseleave",
									function() {
										if ($(this).find("input").attr(
												"checked") != "checked") {
											$(this).find("input").hide();
										}
									});

					$("#expand").live("click", function(e) {
						var data = new Array();
						var i = 0;
						$("#pattern_right th input").each(function() {
							if ($(this).attr("checked") == "checked") {
								data[i] = $(this).attr("value");
								i++;
							}
						});
						if (i != 0) {//expand on column
							expand(data, relation_id, ds_type, ds_id);
						} else {//expand all
							expand2(ds_type, ds_id);
						}
					});

					$("#reset").click(function() {
						location.reload();
					});
					$("#continue").click(function(){
						$("#tabs").tabs("option", "selected", 1);  
						//$("#tabs_2_role").click
						var ds_type = $("#ds_type").val();
						var ds_id = $("#ds_id").val();
						var relation_id = $("#relation_id").val();

						getRelationName(relation_id);
						getPattern_role(relation_id);
						getRelRoles(relation_id);
						getResult_role(relation_id, ds_type, ds_id);
					});
					$(".triple input").live("click", function() {
						var s = $(this).parent().attr("s");
						var p = $(this).parent().attr("p");
						var o = $(this).parent().attr("o");
						var data = {};
						data["s"] = s;
						data["p"] = p;
						data["o"] = o;
						data["id"] = relation_id;
						data["ds_type"] = ds_type;
						data["ds_id"] = ds_id;
						data = JSON.stringify(data);
						data = ("data=" + encodeURIComponent(data));
						$("#selExpand").dialog("close");
						$("#onloading").dialog("open");
						$.ajax({
							url : "/sview/expandAdd",
							async : true,
							data : data,
							dataType : "json",
							type : "post",
							success : function(result) {
								$("#onloading").dialog("close");
								getPattern(relation_id);
								getResult(relation_id, ds_type, ds_id);
							}
						});
					});

					$("#pattern_right th input").live("change", function() {
						if ($(this).attr("checked") == "checked") {
							$("#expand").show();
						} else {
							var has = false;
							$("#pattern_right th input").each(function() {
								if ($(this).attr("checked") == "checked") {
									has = true;
								}
							});
							if (has == false) {
								$("#expand").hide();
							}
						}
					});

					$("#pattern_right td")
							.live(
									"mouseenter",
									function() {
										$(this).css("background-color",
												"#EEEEEE");
										var showText;
										var lang=$("#display-lang").val();
										if(lang == 'en'){
											showText = "bind";
										}else if(lang == 'zh'){
											showText = "绑定";
										}
										var bind = $("<a href='javascript:void(0)' class='bind'>"+showText+"</a>");
										$(this).append(bind);
									});

					$("#pattern_right td").live("mouseleave", function() {
						$(this).css("background-color", "transparent");
						$(this).find("a").remove();
					});

					$(".bind").live("click", function() {
						var v = $(this).parent().attr("v");
						var value = $(this).parent().attr("name");
						var data = {};
						data["v"] = v;
						data["value"] = value;
						data["rel_id"] = relation_id;
						data = JSON.stringify(data);
						data = ("data=" + encodeURIComponent(data));
						$.ajax({
							url : "/sview/variableBind",
							async : true,
							data : data,
							dataType : "json",
							type : "post",
							success : function(result) {
								getPattern(relation_id);
								getResult(relation_id, ds_type, ds_id);
							}
						});
					});
					var dtitle;
					var cancel_btn;
					if(lang == 'en'){
						cancel_btn = "Cancel";
						dtitle = "Select A Triple Pattern to Expand";
					}else if(lang == 'zh'){
						cancel_btn = "取消";
						dtitle = "请选择一个三元组模式进行扩展";
					}
					$("#selExpand").dialog({
						bgiframe : true,
						autoOpen : false,
						draggable : false,
						closeOnEscape : false,
						closeText : false,
						resizable : false,
						width : 800,
						height : 500,
						title : dtitle,
						modal : true,
						buttons : [{
							text: cancel_btn,
							click : function() {
								$(this).dialog("close");
							}
						}]
					});
					$("#selExpand").parent().appendTo("#dialog_target");
				});

function initializeNew() {
	var data = {};
	data["id"] = "0";
//	data["gid"] = $("#gid").val();
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$.ajax({
		url : "/sview/relationIntoSession",
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			$("#expand").show();
		}
	});
}

function initialize(relation_id, ds_type, ds_id) {
	var data = {};
	data["id"] = relation_id;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$.ajax({
		url : "/sview/relationIntoSession",
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			getPattern(relation_id);
			getResult(relation_id, ds_type, ds_id);
		}
	});
}

function getPattern(relation_id) {
	var data = {};
	data["id"] = relation_id;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$
			.ajax({
				url : "/sview/relationPattern",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					var array = result["result"];
					var showText;
					var lang = $("#display-lang").val();
					if(lang == 'en'){
						showText = "reset";
					}else if(lang == 'zh'){
						showText = "重置";
					}
					$("#pattern_left").html("<div id='pattern_reset'><input type='button' class='button-bg' value='"+showText+"' id='reset'></input></div>");
					$("#reset").click(function() {
						location.reload();
					});
					for ( var i = 0; i < array.length; i++) {
						var triple = array[i];
						var stext = new String(triple["stext"]);
						var ptext = new String(triple["ptext"]);
						var otext = new String(triple["otext"]);
						var s = new String(triple["s"]);
						var p = triple["p"];
						var o = new String(triple["o"]);
						var s_dis = stext;
						var p_dis = ptext;
						var o_dis = otext;
						if (s.indexOf("u") == 0) {
							s_dis = getLocalname(stext);
						}
						p_dis = getLocalname(ptext);
						if (o.indexOf("u") == 0) {
							o_dis = getLocalname(otext);
						}
						var canDel = triple["canDel"];
						var tripleDIV = $("<div class='triple'></div>");
						tripleDIV.attr("s", s);
						tripleDIV.attr("p", p);
						tripleDIV.attr("o", o);
						$("#pattern_left").append(tripleDIV);
						var disDIV = $("<span></span>");
						disDIV.html("(<span title='" + stext + "'>" + s_dis
								+ "</span>,<span title='" + ptext + "'>"
								+ p_dis + "</span>,<span title='" + otext
								+ "'>" + o_dis + "</span>)");
						tripleDIV.append(disDIV);
						if (eval(canDel) == true) {
							var delDIV = $("<a class='del' href='javascript:void(0)'><img src='/sview/icons/edit_cancel.png' border='0'/></a>");
							tripleDIV.append(delDIV);
						}
					}
				}
			});
}

function expand2(ds_type, ds_id) {
	var data = {};
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$("#onloading").dialog("open");
	$
			.ajax({
				url : "/sview/relationExpandFromNew",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					var lang = $("#display-lang").val();
					$("#onloading").dialog("close");
					$("#selExpand").dialog("open");
					var array = result["result"];
					$("#selExpand").html("");
					for ( var i = 0; i < array.length; i++) {
						var triple = array[i];
						var stext = new String(triple["stext"]);
						var ptext = new String(triple["ptext"]);
						var otext = new String(triple["otext"]);
						var s = new String(triple["s"]);
						var p = triple["p"];
						var o = new String(triple["o"]);
						var s_dis = stext;
						var p_dis = ptext;
						var o_dis = otext;
						if (s.indexOf("u") == 0) {
							s_dis = getLocalname(stext);
						}
						p_dis = getLocalname(ptext);
						if (o.indexOf("u") == 0) {
							o_dis = getLocalname(otext);
						}
						var tripleDIV = $("<div class='triple'></div>");
						tripleDIV.attr("s", s);
						tripleDIV.attr("p", p);
						tripleDIV.attr("o", o);
						$("#selExpand").append(tripleDIV);
						var disDIV = $("<span></span>");
						disDIV.html("(<span title='" + stext + "'>" + s_dis
								+ "</span>,<span title='" + ptext + "'>"
								+ p_dis + "</span>,<span title='" + otext
								+ "'>" + o_dis + "</span>)");
						var showText;
						if(lang == 'en'){
							showText = "choose";
						}else if(lang == 'zh'){
							showText = "选择";
						}
//						var expandChoose = $("<input type='button' class='button-bg' value='"+showText+"'></input");
						var expandChoose = $("<input type='button' class='button-bg' value='"+showText+"'></input>");
						tripleDIV.append(disDIV);
						tripleDIV.append(expandChoose);
					}
				}
			});
}

function expand(columns, relation_id, ds_type, ds_id) {//expand on column
	var cols = "";
	for ( var x in columns) {
		cols += columns[x] + ",";
	}
	if (cols.length > 0) {
		cols = cols.substring(0, cols.length - 1);
	}
	var data = {};
	data["id"] = relation_id;
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id;
	data["vars"] = cols;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$("#onloading").dialog("open");
	$
			.ajax({
				url : "/sview/relationExpand",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					var lang = $("#display-lang").val();
					$("#onloading").dialog("close");
					$("#selExpand").dialog("open");
					var array = result["result"];
					$("#selExpand").html("");
					for ( var i = 0; i < array.length; i++) {
						var triple = array[i];
						var stext = new String(triple["stext"]);
						var ptext = new String(triple["ptext"]);
						var otext = new String(triple["otext"]);
						var s = new String(triple["s"]);
						var p = triple["p"];
						var o = new String(triple["o"]);
						var s_dis = stext;
						var p_dis = ptext;
						var o_dis = otext;
						if (s.indexOf("u") == 0) {
							s_dis = getLocalname(stext);
						}
						p_dis = getLocalname(ptext);
						if (o.indexOf("u") == 0) {
							o_dis = getLocalname(otext);
						}
						var tripleDIV = $("<div class='triple'></div>");
						tripleDIV.attr("s", s);
						tripleDIV.attr("p", p);
						tripleDIV.attr("o", o);
						$("#selExpand").append(tripleDIV);
						var disDIV = $("<span></span>");
						disDIV.html("(<span title='" + stext + "'>" + s_dis
								+ "</span>,<span title='" + ptext + "'>"
								+ p_dis + "</span>,<span title='" + otext
								+ "'>" + o_dis + "</span>)");
						var showText;
						if(lang == 'en'){
							showText = "choose";
						}else if(lang == 'zh'){
							showText = "选择";
						}
//						var expandChoose = $("<input type='button' class='button-bg' value='"+showText+"'></input");
						var expandChoose = $("<input type='button' class='button-bg' value='"+showText+"'></input>");
						tripleDIV.append(disDIV);
						tripleDIV.append(expandChoose);
					}
				}
			});
}

function getResult(relation_id, ds_type, ds_id) {
	var data = {};
	data["id"] = relation_id+"";
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["dis_all"] = "all";
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$("#onloading").dialog("open");
	$
			.ajax({
				url : "/sview/relationResult",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					var lang = $("#display-lang").val();
					$("#onloading").dialog("close");
					var showText;
					if(lang == 'en'){
						showText = "expand";
					}else if(lang == 'zh'){
						showText = "扩展";
					}
					var expandaa = $("<input type='button' class='button-bg' value='"+showText+"' id='expand'></input>");
					expandaa.hide();
					$("#pattern_right").html(expandaa);
					var table = $("<table></table>");
					$("#pattern_right").append(table);
					var array = result["result"];
					var tableTitle = array[0];
					var trhead = $("<tr></tr>");
					table.append(trhead);
					for ( var i = 0; i < tableTitle.length; i++) {
						var column = tableTitle[i];
						var v = column["v"];
						var th = $("<th></th>");
						th.text(v);
						var ckbox = $("<input type='checkbox' class='checkInput' name='"+showText+"'></input>");
//						ckbox.hide();
						ckbox.attr("value", v);
						th.prepend(ckbox);
						trhead.append(th);
					}
					for ( var i = 1; i < array.length; i++) {
						var trbody = $("<tr></tr>");
						table.append(trbody);
						var item = array[i];
						for ( var j = 0; j < item.length; j++) {
							var ele = item[j];
							var v = ele["v"];
							var name = new String(ele["name"]);
							var content = ele["content"];
							var dis = ele["displayName"].trim();
							if(dis.length==0){
								dis = content;
							}
//							alert("dis:"+dis);
//							var dis = content;
//							if (name.indexOf("u") == 0) {
//								dis = getLocalname(content);
//							}
							var td = $("<td></td>");
							trbody.append(td);
							td.attr("v", v);
							td.attr("name", name);
							var div = $("<span></span>");
							div.attr("title", content);
							div.text(dis);
							td.append(div);
						}
					}
				}
			});
}